package net.entelijan.cobean.data.client;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import net.entelijan.cobean.data.naming.INamingClient;

public enum Profile {

	GLASSFISH_21(//
			"net.entelijan.cobean.data.naming.glassfish.GlassfishNamingClient"), //
	JBOSS_510(//
			"net.entelijan.cobean.data.naming.jboss.JbossNamingClient");

	private static Log log = LogFactory.getLog(Profile.class);
	private String className;

	private Profile(String className) {
		this.className = className;
	}

	public INamingClient createNamingClient() {
		try {
			Class<?> clazz = Class.forName(className);
			return (INamingClient) clazz.newInstance();
		} catch (Exception e) {
			throw new IllegalStateException(
					"Could not instantiat naming client from '" + className
							+ "' because: " + e.getMessage(), e);
		}
	}

	public static List<Profile> findActiveProfiles() {
		ArrayList<Profile> re = new ArrayList<Profile>();
		for (Profile p : Profile.values()) {
			try {
				Class.forName(p.className);
				re.add(p);
			} catch (ClassNotFoundException e) {
				log.debug("Could not create naming client for " + p
						+ " because: " + e.getMessage(), e);
			}
		}
		return re;
	}

}
